package mi.payment.transaction.sale;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import mi.payment.CardType;
import mi.payment.R;
import mi.payment.bean.db.TradeData;
import mi.payment.db.dao.PayDetail;
import mi.payment.settings.TransactionOtherOperation;
import mi.payment.transaction.V2InputPasswordActivity;
import mi.payment.ui.base.PageRoute;
import mi.payment.utils.transaction.ConsumptionTask;
import sunmi.payment.ui.dialog.LoadDialog;

/**
 * 消费模块 -- 输入密码界面
 */
public class ConsumeInputPWDActivity extends V2InputPasswordActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        startTransactionProcess();
    }

    private void initView() {
        mTvTitle.setText(R.string.consume);

        // IC/NFC是否显示卡号( 磁卡不受控制、只有消费、预授权过程中生效、其他交易不生效)
        boolean isShowCardNo = CardType.MAGNETIC.getValue() == mTradeData.getPayDetail().cardType ||
                TransactionOtherOperation.getInstance().getTransactionOtherSettings().isICCardShowCardNumber();
        showCardNoView(isShowCardNo);
    }

    @Override
    @SuppressLint("StaticFieldLeak")
    public void onStartOnline() {
        // 消费
        mTradeData.getPayDetail().setTransactionType(1);
        ConsumptionTask task = new ConsumptionTask(this) {

            @Override
            protected void onPostExecuteImpl(TradeData tradeData) {
                PayDetail resPay = tradeData.getPayDetail();
                PageRoute.startAction(mActivity, PageRoute.TYPE_CONSUME, resPay);
            }

            @Override
            protected void onTaskTimeoutImpl() {
                PageRoute.startTimeoutAction(mActivity, PageRoute.TYPE_CONSUME, mTradeData.payDetail);
            }

        };
        LoadDialog loadingDialog = getLoadingDialog(R.string.load_connecting);
        task.setLoadDialog(loadingDialog);
        task.setNeedAuthorization(true);
        task.execute(mTradeData);
    }

    public static void startAction(Context context) {
        Intent intent = new Intent(context, ConsumeInputPWDActivity.class);
        context.startActivity(intent);
    }


}
